﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LandNotepad
{
    public partial class NotepadForm : Form
    {
        public NotepadForm()
        {
            InitializeComponent();
        }

        private void NotepadForm_Load(object sender, EventArgs e)
        {
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(openpath))
            {
                //1.保存配置文件
                File.WriteAllText(FontConfig.filePath, $"{this.TxtContent.Font.FontFamily},{this.TxtContent.Font.Size},{this.TxtContent.Font.Style}");
                //2.保存内容
                File.WriteAllText(openpath, this.TxtContent.Text);
            }
            else
                this.另存为ToolStripMenuItem_Click(sender, e);

        }

        private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var ff = new FontStyle();
            ff.FontSave += Ff_FontSave;//注册委托事件（按两下Tab ）
            ff.ShowDialog();
        }

        
        private void Ff_FontSave(Font font)
        {
            this.TxtContent.Font = font;
        }

        /// <summary>
        /// 另存为
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog() {Filter="(*.txt文件)|*.txt"};
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                //文件持久化
                File.WriteAllText(sfd.FileName, this.TxtContent.Text);
            }
                

        }

        private string openpath="";
        /// <summary>
        /// 打开文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog fopen = new OpenFileDialog();
            if (fopen.ShowDialog() == DialogResult.OK)
            {
                this.TxtContent.Text = File.ReadAllText(fopen.FileName);
                this.openpath = fopen.FileName;
            }
        }

        private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog color = new ColorDialog();
            if (color.ShowDialog() == DialogResult.OK)
                this.TxtContent.ForeColor = color.Color;
        }

        private void 背景颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog color = new ColorDialog();
            if (color.ShowDialog() == DialogResult.OK)
                this.TxtContent.BackColor = color.Color;
        }
    }
}
